package com.bqtt.tumor.demo.presentation.controller;

import java.beans.PropertyEditor;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.bqtt.tumor.demo.business.HealthUserService;
import com.bqtt.tumor.demo.business.UserService;
import com.bqtt.tumor.demo.entity.HealthUser;
import com.bqtt.tumor.demo.entity.SystemUser;


@Controller
@SessionAttributes(value={"currentUser","condition"})
public class login {

	private static final Logger log = LoggerFactory.getLogger(login.class);
	
	@RequestMapping("/")
	public String showHomePage(ModelMap model,@ModelAttribute("currentUser") SystemUser currentUser){	
		
		
		if(currentUser.getSessionState() == 1)
		{//已经登入
			return checkUser(model,currentUser, null);
		}
		
		if(!model.containsAttribute("user"))
		{  // for spring form entity
			model.addAttribute("user", new SystemUser());
		}
		
		return "login";
	}
	
	@RequestMapping("/login_confirm")
	public String checkUser(ModelMap model, @Valid @ModelAttribute("user") SystemUser user,BindingResult bindingResult)
	{
		if(user==null)
		{
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		int status = UserService.userAuthentication(user);
		

		//公共模型参数
		if(status!=0)
		{
			 //登入成功后，添加到 session中。
			user.setSessionState(1); 
			model.addAttribute("currentUser", user);
			
		}
		
		
		
	    if(status == 1 || status == 3 || status == 4)
		{
	    	if(!model.containsAttribute("condition"))
			     model.addAttribute("condition", new HealthUser());
			
			if(!model.containsAttribute("new_health_user"))
			     model.addAttribute("new_health_user", new HealthUser());		
			if(!model.containsAttribute("edit_health_user"))
			     model.addAttribute("edit_health_user", new HealthUser());	
			
		    model.addAttribute("health_user_list", HealthUserService.searchByUserCondition((HealthUser)model.get("condition"), 20, 1));
		    
	    	model.addAttribute("isAddNewShow","false");
	    	
	    	return "forward:admin/searchHealthUser";
	    	
		}
		else if(status == 5 && status == 6)
		{
			return "doctor";
		}
		else if(status ==2)
		{
			return "register_admin";
		}
		else
		{
			return "login";
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
}
